<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="no-cache">
<title>Insert title here</title>
<%
	//防止浏览器产生缓存
	response.setHeader("Pragma","No-Cache"); 
	response.setHeader("Cache-Control","No-Cache"); 
	response.setDateHeader("Expires", 0); 

	//定义基础路径，从DispatcherServlet获取数据
	String path = request.getContextPath() + "/";	
	String resourcesPath = path + "resources/";
	String omPath = resourcesPath + "operamasks-ui-2.0/";		
	
%>
<link href="<%=omPath%>css/default/om-default.css" rel="stylesheet"	type="text/css" />
<script src="<%=omPath%>js/jquery.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/jquery.xml2json.js" type="text/javascript"></script>
<script src="<%=omPath%>js/operamasks-ui.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/om-dialog-url.js" type="text/javascript"></script>
<style type="text/css">
	.search-button{
	padding-left: 50px;
	}
   	.myinput input{
    border:1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput2 input{
    border:0.1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput input:focus{border: 1px solid #3A76C2;}
   	.myinput2 input:focus{border: 0.1px  #3A76C2;}

   .mytable td{
   padding-top: 5px;
   }
   .mytable .mytable-left{
   text-align: right;
   width : 100px;
   }
  .mytable .mytable-right{
   width : 150px;
   }
</style>
<script type="text/javascript">
//集中获取ModelAndView数据
var path = "${pageContext.request.contextPath}" + "/";
var searchField =null;// 生成搜索字段
var searchButton = null; // 生成搜索按钮
var fromStockContentGrid = null;// 生成移动前料仓
var moveWeight = null;// 生成数值控件
var moveButton = null;// 生成工具
var toStock = null;// 选择源料仓的input
var toDropList = null;// 源料仓的下拉表格所在的div容器
var toStockGrid = null;// 源料仓的下拉表格
var toStockContentGrid = null;// 生成移动后料仓
var toStockDetail = null; // 生成移动后料仓的相关信息
var systemtips = null;// 生成系统提示对话框

//引用通用条件查询网址，设置表格提交的网址
var searchUrl = path + "rm/filterQuery.html";
var moveUrl = path + "rm/MatMoveCtl/matUnload.html";// 定义物料移动的网址

$(function(){
	initVar();	
	loadLayout();
	loadSearchPanel();
	loadGrid();
	loadMove();
	loadShowToStockGrid();
});

function initVar(){
	systemtips = $('#systemtips');// 生成系统提示对话框
	moveButton = $('#moveButton'); // 生成工具
	moveWeight = $('#moveWeight');// 生成数值控件
	fromStockContentGrid = $("#fromStockContentGrid");// 生成移动前料仓
	searchFiled = $("#search-field");// 生成搜索字段
	searchButton = $('#search-button'); // 生成搜索按钮
	toStock = $("#toStock");// 选择源料仓的input
	toDropList = $("#toPopGrid");// 源料仓的下拉表格所在的div容器
	toStockGrid = $("#toStockGrid");// 源料仓的下拉表格
	toStockContentGrid = $("#toStockContentGrid");// 生成移动后料仓
	toStockDetail = $("#toStockDetail"); // 生成移动后料仓的相关信息
}

function loadLayout(){
	$("#pageLayout").omBorderLayout({
		panels : [{
			id : "pageLayout1",
			region : "north",
			header : false,
			height : 310,
		},{
			id : "pageLayout2",
			region : "center",
			header : false,
		},{
			id : "pageLayout3",
			region : "south",
			header : false,
			height : 310,
		}],
		spacing : 3,
	});
	$("#fromLayout").omBorderLayout({
		panels : [{
			id : "fromLayout1",
			region : "west",
			collapsible : true,
			width : 400,
		},{
			id : "fromLayout2",
			region : "center",
			header : false,
		}],
		spacing : 3,
	});
	$("#toLayout").omBorderLayout({
		panels : [{
			id : "toLayout1",
			region : "west",
			collapsible : true,
			width : 400,
		},{
			id : "toLayout2",
			region : "center",
			header : false,
		}],
		spacing : 3,
	});
}

function loadSearchPanel(){
	$('#search-field input').each(function() {
		$(this).keyup(function(event) {
			if (event.keyCode == 13) {
				search();
			}
		});
	});
	$('#queryDeliveryType').omCombo({
		dataSource : [ {
			text : '直供',
			value : '直供'
		}, {
			text : '配送',
			value : '配送'
		} , {
			text : '调拨',
			value : '调拨'
		} ],
		width : '162px'
	});
	$("#queryReceiveTimeS").omCalendar({showTime : true});
	$("#queryReceiveTimeE").omCalendar({showTime : true});
	$("#queryReceiveTimeS").css("width","140px");
	$("#queryReceiveTimeE").css("width","140px");
	searchButton.omButton({
		icons:{left : path + 'resources/sm/images/search.png'},
		width:70,
		onClick:function(){
			search();
		}
	});
}

function loadGrid(){
	fromStockContentGrid.omGrid({
		limit : 30,
		singleSelect : true,
		height:306,
		title : "计量单数据",
		colModel : [
				{header : '计量单号', name : 'measureId', align : 'center', width : '130', sort:'serverSide'},
				{header : '物料编码', name : 'materialId', align : 'center', width : '80', sort:'serverSide'},
				{header : '物料描述', name : 'materialDesc', align : 'center', width : '160', sort:'serverSide'},
				{header : '净重(KG)', name : 'netWeight', align : 'center', width : '100', sort:'serverSide'},
				{header : '剩余净重(KG)', name : 'surplusNetWeight', align : 'center', width : '100', sort:'serverSide'},
				{header : '毛重(KG)', name : 'grossWeight', align : 'center', width : '100', sort:'serverSide'},
				{header : '皮重(KG)', name : 'tareWeight', align : 'center', width : '100', sort:'serverSide'},
				{header : '收货时间', name : 'receiveTime', align : 'center', width : '160', sort:'serverSide'},
				{header : '状态', name : 'status', align : 'center', width : '80', sort:'serverSide'},
				{header : '主车号', name : 'cartNumber', align : 'center', width : '80', sort:'serverSide'},
				{header : '进厂类型', name : 'deliveryType', align : 'center', width : '100', sort:'serverSide'},
		            ],
        onRowClick : function(rowIndex, rowData, event) {
			moveWeight.val(rowData.surplusNetWeight);
		}
	});
	toStockGrid.omGrid({
		limit : 0,
		singleSelect : true,
		showIndex : false,
		width : 500,
		height: 190,
		colModel : [
				{header : '料仓ID', name : 'storageId', align : 'center', width : '40',},
				{header : '料仓名称', name : 'storageName', align : 'center', width : '140',},
				{header : '物料编码', name : 'materialId', align : 'center', width : '100',},
				{header : '当前重量(KG)', name : 'totalWgt', align : 'center', width : '80',},
				{header : '料仓容量(KG)', name : 'volume', align : 'center', width : '80',},
		            ],
        onRowDblClick : function(rowIndex, rowData, event) {
			toStock.val(rowData.storageName);
			$(":input",toStockDetail).each(function(){
				$(this).val(rowData[this.name]);
			});
			reloadToStockContentGrid(rowData);
			toDropList.hide();
		}
	});
	toStockContentGrid.omGrid({
		limit : 30,
		singleSelect : true,
		height:306,
		title : "料仓数据",
		colModel : [
				{header : '料仓编码', name : 'storageId', align : 'center', width : '60'},
				{header : '计量单号', name : 'measureId', align : 'center', width : '160'},
				{header : '物料编码', name : 'materialId', align : 'center', width : '80'},
				{header : '物料描述', name : 'materialDesc', align : 'center', width : '120'},
				{header : '重量(KG)', name : 'weight', align : 'center', width : '80'},
				{header : '下料时间', name : 'timeStamp', align : 'center', width : '160'},
				{header : '质检批', name : 'qualityBatch', align : 'center', width : '90'},
		            ],
	});
}

function reloadToStockContentGrid(rowData){
	var group = {
			op : "and",
			rules : [ {
				op : "equal",
				field : "storageId",
				value : rowData.storageId,
				type : "string"
			} ]
		};
	if (group.rules.length) {
		var extraData = {
				"domain":"SmRmStorageContent",
				"sortBy":"timeStamp",
				"sortDir":"desc",
				"rules":JSON.stringify(group)
		};
		toStockContentGrid.omGrid({
			dataSource : searchUrl,
			extraData : extraData
		}, 'reload');
	}
}

function search(){
	var rules =[], i=0;
	$(':input','#searchTable').each(function() {
		if($.trim(this.name)!="")
			if( $(this).val() != null &&  $.trim($(this).val()) != "" 
					&& typeof($(this).attr('op')) != "undefined" && typeof($(this).attr('ty')) != "undefined")
				rules[i++] = {op : $(this).attr('op'), field : $(this).attr('name'), value : $(this).val(), type : $(this).attr('ty')};
	});
	rules[i++] = {op : 'equal', field : 'status', value : 'RECEIVE', type : 'string'};
	rules[i++] = {op : 'equal', field : 'anaDecide', value : 'Y', type : 'string'};
	var group ={
			op : "and",
			rules : rules
	};
	var extraData = {
			"domain":"SmRmMeasureDoc",
			"rules":i==0?null:JSON.stringify(group),
	};
	fromStockContentGrid.omGrid({
		dataSource : searchUrl,
		extraData : extraData, 
		method:'POST'
	}, 'reload');
}

function loadMove(){
	moveWeight.omNumberField({
		allowDecimals : true,
		allowNegative : false,
		decimalPrecision : 1
	});
	moveButton.omButton({
		onClick : function(event) {
			onStoreBtn();
		}
	});
	onStoreBtn = function() {
		try {
			var selections = fromStockContentGrid.omGrid('getSelections', true);
			if (selections.length != 1) {
				$.omMessageBox.alert({
					content : '请先选择一条待下料的计量单',
				});
				return false;
			}
		} catch (e) {
			$.omMessageBox.alert({
				content : '请先选择一条待下料的计量单',
			});
			return false;
		}

		var toStorageName = toStock.val();
		if (toStorageName == null) {
			$.omMessageBox.alert({
				content : '请选择下料的料仓',
			});
			return false;
		}
		if (moveWeight.val() == null || moveWeight.val() == '') {
			$.omMessageBox.alert({
				content : '请输入移动重量',
			});
			return false;
		}
		var fromRow = selections[0];
		if (moveWeight.val() > fromRow.surplusNetWeight) {
			$.omMessageBox.alert({
				content : '移动重量超出剩余重量',
			});
			return false;
		}
		var weight = moveWeight.val();
		moveMat(fromRow, $("#storageId", toStockDetail).val(), weight);

	};

	moveMat = function(fromRow, toStorageId, weight) {
		var info = {
			"measureId" : fromRow.measureId,
			"toStorageId" : toStorageId,
			"totalWgt" : $("#totalWgt", toStockDetail).val(),
			"weight" : weight
		};
		$.post( moveUrl,info, 
				function(data, textStatus)
				{
			     if( data.success )
					{
			    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
			    	 var curWgt = $("#totalWgt", toStockDetail).val();
					 curWgt = new Number(curWgt) + new Number(weight);
					 $("#totalWgt", toStockDetail).val(curWgt);
			    	 fromStockContentGrid.omGrid( "reload" );
			    	 toStockContentGrid.omGrid( "reload" );
					}
			     else
			    	 {
						$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
			    	 }						
				});
	};
}

function loadShowToStockGrid(){
	toStock.attr('readonly', 'readOnly').focus(function(){
		var inputOffset = toStock.offset();
		var toDropListTop = inputOffset.top + toStock.outerHeight();
		var toDropListLeft = inputOffset.left;
		toDropList.css({
			top : toDropListTop,
			left : toDropListLeft
		});
		// 根据网址搜索目的料仓
		var group = {
			"rules" : [ {
				"field" : "storageType",
				"op" : "equal",
				"type" : "string",
				"value" : "B"
			}],
			"op" : "and"
		};
		var extraData = {
				"domain":"VSmRmStorage",
				"sortBy":"storageId",
				"sortDir":"asc",
				"rules":JSON.stringify(group)
		};
		toStockGrid.omGrid({
			dataSource : searchUrl,
			extraData : extraData, 
			method:'POST'
		}, 'reload');
		toDropList.show();
	});
	toDropList.mousedown(function(e) {
		e.stopPropagation();
	});
	$(document.body).mousedown(function() {
		toDropList.hide();
	});
	toDropList.hide();
}
</script>

<title>入库</title>
</head>
<body>
 <div id="pageLayout" style="height:685px;">
    	<div id="pageLayout1">
    		<div id="fromLayout" style="height:100%">
    			<div id="fromLayout1">     						
    				<div id="search-field">
    				<table id="searchTable" class="mytable">
    					<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">进厂类型:</font></span></td>
							<td class="mytable-right"><span class="myinput"><input id="queryDeliveryType" name="deliveryType" op="equal" ty="string" type="text" /></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">计量单号:</font></span></td>
							<td class="mytable-right"><span class="myinput"><input id="queryMeasureId" name="measureId" op="equal" ty="string" type="text" /></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料编码:</font></span></td>
							<td class="mytable-right"><span class="myinput"><input id="queryMaterialId" name="materialId" op="equal" ty="string" type="text" /></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">收货时间从:</font></span></td>
							<td class="mytable-right"><span class="myinput2"><input id="queryReceiveTimeS" name="receiveTime" op="equal" ty="string" type="text" /></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">收货时间到:</font></span></td>
							<td class="mytable-right"><span class="myinput2"><input id="queryReceiveTimeE" name="receiveTime" op="equal" ty="string" type="text" /></span></td>
						</tr>
					</table>
    				</div>		
					<br />
					<div class="search-button"><span id="search-button">搜索</span></div>
    			</div>
    			<div id="fromLayout2">
    				<table id="fromStockContentGrid"></table>
    			</div>
    		</div>
		</div>
    	<div id="pageLayout2" style="padding: 0 10px;">
    		<div style="margin-left:auto;margin-right:auto;margin-top:20px;width:400px;">
    			<label>重量：</label>
    			<input id="moveWeight"></input>
    			<label>KG</label>
    			<button id="moveButton" value="移至料仓"></button>
			</div>    		
		</div>    	
    	<div id="pageLayout3">
    		<div id="toLayout" style="height:100%">
    			<div id="toLayout1">
    				<form id="temp1"> 
    				<table class="mytable">
    					<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">选择料仓:</font></span></td>
							<td class="mytable-right"><span class="myinput"><input id="toStock" type="text" class="field"/></span></td>
						</tr>
					</table>  			
    				</form>    			
    				<form id="toStockDetail">
    				<table class="mytable">
    					<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓编码:</font></span></td>
							<td class="mytable-right"><span class="myinput"><input id="storageId" name="storageId" type="text" class="field" disabled="disabled"/></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料编码:</font></span></td>
							<td class="mytable-right"><span class="myinput"><input id="materialId" name="materialId" type="text" class="field" disabled="disabled"/></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料描述:</font></span></td>
							<td class="mytable-right"><span class="myinput"><input name="materialDesc" id="materialDesc" type="text" class="field" disabled="disabled"/></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">当前重量(KG):</font></span></td>
							<td class="mytable-right"><span class="myinput"><input id="totalWgt" name="totalWgt" type="text" class="field" disabled="disabled"/></span></td>
						</tr>
						<tr>
							<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓容量(KG):</font></span></td>
							<td class="mytable-right"><span class="myinput"><input name="volume" id="volume" type="text" class="field" disabled="disabled"/></span></td>
						</tr>
					</table>
    				</form>			
    			</div>
    			<div id="toLayout2">
    				<table id="toStockContentGrid"></table>
    			</div>
    		</div>    		
		</div>
 </div> 
 <div id="fromPopGrid" class='omcombo-ct' style='position: absolute; display: block; left: 0;'>
	<table id="fromStockGrid"></table>
 </div> 
 <div id="toPopGrid" class='omcombo-ct' style='position: absolute; display: block; left: 0;'>
	<table id="toStockGrid"></table>
 </div>
 <div id="systemtips" title="系统提示信息"></div> 
</body>
</html>